<?xml version="1.0"?>
<!DOCTYPE stylesheet [
<!ENTITY brvbar "<xsl:text>&#166;</xsl:text>">
<!ENTITY comma "<xsl:text>&#44;</xsl:text>">
<!ENTITY space "<xsl:text> </xsl:text>">
<!ENTITY cr "<xsl:text>
</xsl:text>">
]>
	<!-- Version 6 -->
<xsl:stylesheet id="v6" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:dsml="http://www.dsml.org/DSML"
  version="2.0">

  <xsl:key name="dn" match="entry" use="@dn" />
  <xsl:key name="class" match="dsml:class" use="@id" />

  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="dsml">
  <xsl:apply-templates select="directory-entries" />
  </xsl:template>

    <xsl:template match="directory-entries">
      <xsl:element name="entries">
        <!--Apply templates to each top-level entry -->
        <xsl:apply-templates select="./entry">
          <xsl:with-param name="dn" select="./@dn" />
        </xsl:apply-templates>
   &cr;
      </xsl:element>
    </xsl:template>

    <xsl:template match="entry">
      <xsl:param name="dn" />
      <!--The xml element corresponds to either the "structural" or "auxilary" objectclass-->
      <xsl:variable name="classid_struct">
        <xsl:value-of select="key('class',objectclass/oc-value)[@type='structural']/@id" />
      </xsl:variable>
      <xsl:variable name="classid">
        <xsl:if test="$classid_struct != ''">
          <xsl:value-of select="$classid_struct" />
        </xsl:if>
        <xsl:if test="$classid_struct = ''">
          <xsl:value-of select="key('class',objectclass/oc-value)[@type='auxiliary']/@id" />
        </xsl:if>
      </xsl:variable>
      <xsl:if test="$classid != ''">
        <!-- The element name is the ldap classname-->
    &cr;
        <xsl:element name="{$classid}">
          <!-- Write the dn as an attribute-->
          <xsl:attribute name="dn">
            <xsl:value-of select="@dn" />
          </xsl:attribute>
          <!--Write the ldap attributes-->
          <xsl:apply-templates select="./attr" />
          <!--Apply templates for the entries that are children of this one-->
          <xsl:apply-templates select="./entry">
            <xsl:with-param name="dn" select="./@dn" />
          </xsl:apply-templates>    
    &cr;
        </xsl:element>
      </xsl:if>
    </xsl:template>

    <xsl:template match="attr">
      <xsl:variable name="attrname">
        <xsl:value-of select="@name" />
      </xsl:variable>
      <xsl:attribute name="{$attrname}">
        <xsl:apply-templates select="value" />
      </xsl:attribute>
    </xsl:template>
	
    <xsl:template match="value">
      <xsl:value-of select="." />
      <!--Separate multiple attribute values by broken bar-->
      <xsl:if test="position()!=last()">
        &comma;
      </xsl:if> 
    </xsl:template>

</xsl:stylesheet>
